<?php
/**
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 3 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/

/**
 * Transaction.
 * @package Core
 * @subpackage Database
 * @author kovacsricsi
 */
class Transaction
{
	/**
	 * Name of database connection
	 * @access private
	 * @var string
	 */
    private $_name;

	/**
	 * CNeed rollback or not.
	 * @access private
	 * @var boolean
	 */
	private $_needRollback;

	/**
	 * Constructor tart transaction.
	 * @access public
	 * @param string $name
	 * return void
	 */
    public function __construct($name = "default")
    {
        $this->_name         = $name;
		$this->_needRollback = true;

        QueryHandler::execute("START TRANSACTION;", null, $this->_name);
    }

	/**
	 * Commit transaction.
	 * @access public
	 * @param string $name
	 * return void
	 */
    public function commit()
    {
        QueryHandler::execute("COMMIT;", null, $this->_name);

		$this->_needRollback = false;
    }

	/**
	 * Rollback transaction.
	 * @access public
	 * @param string $name
	 * return void
	 */
    public function rollback()
    {
       QueryHandler::execute("ROLLBACK;", null, $this->_name);

	   $this->_needRollback = false;
    }

	/**
	 * Desctructor rollback transaction if it not committed.
	 * @access public
	 * @return void
	 */
    public function __destruct()
    {
		if ($this->_needRollback == true) {
			$this->rollback();
		}
    }
}

?>
